#include"../Common/Net.hpp"
#include"../Common/Message.hpp"
#include "../Common/Dispatcher.hpp"

// using ConnectionCallback = std::function<void(const BaseConnection::ptr &conn)>;
// using MessageCallback = std::function<void(const BaseConnection::ptr &conn, const BaseMessage::ptr &buf)>;
// using CloseCallback = std::function<void(const BaseConnection::ptr &conn)>;


void OnRpcMessage(const zgwrpc::BaseConnection::ptr &conn, const zgwrpc::RpcRequest::ptr &req)
{
    Json::Value params=req->params(), result;
    int num1 = params["num1"].asInt();
    int num2 = params["num2"].asInt();
    result["sum"] = num1 + num2;
    zgwrpc::RpcResponse::ptr res = zgwrpc::MessageFactory::create<zgwrpc::RpcResponse>();
    res->setId(req->rid());
    
    res->setResult(result);
    res->setRCode(zgwrpc::RCode::RCODE_OK);
    res->setMType(zgwrpc::MType::RSP_RPC);
    conn->send(res);
}


void test1()
{
    zgwrpc::BaseServer::ptr server=zgwrpc::ServerFactory::create(8888);

    zgwrpc::Dispatcher::ptr dispatcher = zgwrpc::DispatcherFactory::create();
    dispatcher->registerHandler<zgwrpc::RpcRequest>(zgwrpc::MType::REQ_RPC, OnRpcMessage);
    server->setMessageCallback(std::bind(&zgwrpc::Dispatcher::dispatch, dispatcher, std::placeholders::_1, std::placeholders::_2));




    server->start();
}

int main()
{
    test1();
    return 0;
}